#include "Sound.h"
#include <HAPI_lib.h>

CSound* CSound::instance=0;

CSound::CSound(void):enableSound(true), errorSound(false)
{}

CSound::~CSound(void){}

CSound& CSound::getInstance(void)
{
	if(instance==0)
	{
		instance= new CSound();
	}
	return *instance;
}

/*function that close and delete the sound component*/
void CSound::Close(void)
{
	delete instance;
	instance=0;
}

/*load a sound. Return false if the sound cannot be load and display a user message. 
If a sound cannot be lound, no sounds is played during the game*/
bool CSound::loadSound(string filename)
{
	int index=(int)this->vectorSound.size();
	if(HAPI->LoadSound(filename,&index))
	{
		vectorSound.push_back(index);
		return true;
	}
	//if the sound didn't load
	HAPI->UserMessage("ERROR: The sound "+filename+" cannot be load. As a result the game will run with no sound.","ERROR SOUND");
	this->errorSound=true;
	return false;
}

/*stop to play a sound*/
void CSound::stopSound(int index)
{
	if(errorSound) return;
	HAPI->StopSound(index);
}

/*play a sound*/
void CSound::playSound(int index,int volume)
{
	//if we can play the sound
	if(!enableSound || errorSound) return;
	//change the volume
	int realVolume;
	if(volume==eVolumeLow)
	{
		realVolume=800;
	}
	else if(volume==eVolumeMedium)
	{
		realVolume=900;
	}
	else
	{
		realVolume=1000;
	}
	HAPI->PlayASound(index,false,realVolume);
}

bool CSound::getEnableSound(void)const
{
	return this->enableSound;
}

void CSound::setEnableSound( bool enableSound)
{
	this->enableSound=enableSound;
}